//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.app.service;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.base.domain.*;
import com.ruoyi.live.base.mapper.*;
import com.ruoyi.live.base.domain.vo.ImportDetail;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.utils.PicUtils;
import com.ruoyi.live.websocket.WebSocketServer;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

@Service
public class ExamImportService {
    @Autowired
    LivePaperImportMapper livePaperImportMapper;
    @Autowired
    LivePaperImportErrorMapper livePaperImportErrorMapper;
    @Autowired
    LivePaperImportProductMapper livePaperImportProductMapper;
    @Autowired
    LivePaperImportProductDetailMapper livePaperImportProductDetailMapper;
    @Autowired
    LiveProductMapper productMapper;
    @Autowired
    LiveProductItemMapper liveProductItemMapper;
    @Autowired
    LiveProductSourceMapper liveProductSourceMapper;
    @Autowired
    LiveBarrageMapper barrageMapper;

    @Autowired
    WebSocketServer webSocketServer;

    public static String url = "";

    public ExamImportService() {
    }

    public void doCheckFile(Long id, HttpServletRequest request) throws Exception {
        LivePaperImport paperImport = (LivePaperImport)this.livePaperImportMapper.selectById(id);
        String filePath = request.getSession().getServletContext().getRealPath("/static/static/data/import/");
        filePath = filePath + paperImport.getFilepath() + "\\" + paperImport.getImportName();
        this.readFilePathErr(filePath, id);
        this.livePaperImportMapper.updateById((new LivePaperImport()).setId(id).setState(3).setError(1));
    }

    public void readFilePathErr(String filePath, Long id) throws Exception {
        String[] filelist = (new File(filePath)).list();

        for(int i = 0; i < filelist.length; ++i) {
            if ((new File(filePath + "\\" + filelist[i])).isDirectory()) {
                this.readOneProduct(filePath + "\\" + filelist[i], filelist[i], id);
            } else if (filelist[i].contains("人物设定")) {
                String person = this.readWord(filePath, filelist[i]);
                this.livePaperImportMapper.updateById((new LivePaperImport()).setId(id).setImportPersona(person));
            }
        }

    }

    public void readOneProduct(String filePath, String productName, Long importId) throws Exception {
        LivePaperImportProduct product = (new LivePaperImportProduct()).setProductName(productName).setState(1).setImportId(importId).setFilepath(productName);
        this.livePaperImportProductMapper.insert(product);
        String[] filelist = (new File(filePath)).list();

        for(int i = 0; i < filelist.length; ++i) {
            if ((new File(filePath + "\\" + filelist[i])).isDirectory()) {
                if ("商品图片".equals(filelist[i])) {
                    this.readAllPic(filePath + "\\" + filelist[i], product.getId(), importId);
                }
            } else if (filelist[i].contains("弹幕问题") && !filelist[i].startsWith("~")) {
                List<String> barrages = this.readExcel(filePath, filelist[i]);
                Iterator var8 = barrages.iterator();

                while(var8.hasNext()) {
                    String barrage = (String)var8.next();
                    this.livePaperImportProductDetailMapper.insert((new LivePaperImportProductDetail()).setContent(barrage).setType(3).setImportId(importId).setProductId(product.getId()));
                }
            } else if (filelist[i].contains("商品介绍") && !filelist[i].startsWith("~")) {
                String productDesc = this.readWord(filePath, filelist[i]);
                this.livePaperImportProductMapper.updateById((new LivePaperImportProduct()).setId(product.getId()).setProductDesc(productDesc));
            }
        }

    }

    public void readAllPic(String filePath, Long productId, Long importId) {
        String[] filelist = (new File(filePath)).list();

        for(int i = 0; i < filelist.length; ++i) {
            if ((new File(filePath + "\\" + filelist[i])).isDirectory()) {
                if ("详情图".equals(filelist[i])) {
                    this.doReadPicXq(filePath + "\\" + filelist[i], productId, importId);
                } else if ("主图".equals(filelist[i])) {
                    this.doReadPicZt(filePath + "\\" + filelist[i], productId, importId);
                }
            }
        }

    }

    public void doReadPicXq(String filePath, Long productId, Long importId) {
        String[] filelist = (new File(filePath)).list();

        for(int i = 0; i < filelist.length; ++i) {
            if ((new File(filePath + "\\" + filelist[i])).isDirectory()) {
                this.savePicXq(filePath, filelist[i], productId, importId);
            }
        }

    }

    public void savePicXq(String filePath, String fileName, Long productId, Long importId) {
        Integer section = this.getSectionType(fileName);
        if (!ICommons.isNullOrEmpty(section)) {
            String[] filelist = (new File(filePath + "\\" + fileName)).list();
            filePath = filePath + "\\" + fileName;

            for(int i = 0; i < filelist.length; ++i) {
                if ((new File(filePath + "\\" + filelist[i])).isFile()) {
                    String filename = filelist[i];
                    String ext = filename.substring(filename.lastIndexOf("."));
                    if (".gif.jpg.jpeg.png".contains(ext)) {
                        String storeName = UUID.randomUUID().toString().replaceAll("-", "") + ext;
                        String filepath = filePath + "\\" + filelist[i];
                        (new File(filepath)).renameTo(new File(filePath + "\\" + storeName));
                        Map<String, Integer> res = PicUtils.getImgSize(filePath + "\\" + storeName);
                        this.livePaperImportProductDetailMapper.insert((new LivePaperImportProductDetail()).setProductId(productId).setFilepath(filelist[i]).setRelativepath(storeName).setType(1).setWidth((Integer)res.get("width")).setHeight((Integer)res.get("height")).setAnswer(section == 7 ? 2 : 1).setSection(section == 7 ? 1 : section).setFirst(2).setImportId(importId));
                    }
                }
            }
        }

    }

    public void doReadPicZt(String filePath, Long productId, Long importId) {
        String[] filelist = (new File(filePath)).list();

        for(int i = 0; i < filelist.length; ++i) {
            if ((new File(filePath + "\\" + filelist[i])).isDirectory()) {
                if ("干扰图".equals(filelist[i])) {
                    this.doSaveReadPicZt(filePath + "\\" + filelist[i], productId, 2, 2, importId);
                } else if ("商品首图".equals(filelist[i])) {
                    this.doSaveReadPicZt(filePath + "\\" + filelist[i], productId, 1, 1, importId);
                } else if ("商品图".equals(filelist[i])) {
                    this.doSaveReadPicZt(filePath + "\\" + filelist[i], productId, 1, 2, importId);
                }
            }
        }

    }

    public void doSaveReadPicZt(String filePath, Long productId, Integer answer, Integer first, Long importId) {
        String[] filelist = (new File(filePath)).list();

        for(int i = 0; i < filelist.length; ++i) {
            if ((new File(filePath + "\\" + filelist[i])).isFile()) {
                String ext = filelist[i].substring(filelist[i].lastIndexOf("."));
                String storeName = UUID.randomUUID().toString().replaceAll("-", "") + ext;
                String filepath = filePath + "\\" + filelist[i];
                (new File(filepath)).renameTo(new File(filePath + "\\" + storeName));
                Map<String, Integer> res = PicUtils.getImgSize(filePath + "\\" + storeName);
                this.livePaperImportProductDetailMapper.insert((new LivePaperImportProductDetail()).setProductId(productId).setFilepath(filelist[i]).setRelativepath(storeName).setType(2).setWidth((Integer)res.get("width")).setHeight((Integer)res.get("height")).setAnswer(answer).setSection(1).setFirst(first).setImportId(importId));
            }
        }

    }

    public Integer getSectionType(String path) {
        if ("属性图".equals(path)) {
            return 1;
        } else if ("特点图".equals(path)) {
            return 2;
        } else if ("整体图".equals(path)) {
            return 3;
        } else if ("特写图".equals(path)) {
            return 4;
        } else if ("配送说明图".equals(path)) {
            return 5;
        } else if ("售后说明图".equals(path)) {
            return 6;
        } else {
            return "干扰图".equals(path) ? 7 : null;
        }
    }

    public String readWord(String filePath, String filename) throws Exception {
        String extString = filename.substring(filename.lastIndexOf("."));
        if (".doc".equals(extString) && !filename.startsWith("~")) {
            return this.readDoc(filePath + "\\" + filename);
        } else if (".docx".equals(extString) && !filename.startsWith("~")) {
            return this.readDocx(filePath + "\\" + filename);
        } else {
            throw new Exception("读取文件失败");
        }
    }

    public String readDoc(String path) throws IOException {
        File file = new File(path);
        String str = "";

        try {
            FileInputStream fis = new FileInputStream(file);
            HWPFDocument doc = new HWPFDocument(fis);
            Range range = doc.getRange();
            str = range.text();
            fis.close();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return str;
    }

    public String readDocx(String path) throws IOException {
        File file = new File(path);
        String str = "";

        try {
            FileInputStream fis = new FileInputStream(file);
            XWPFDocument xdoc = new XWPFDocument(fis);
            XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
            str = extractor.getText();
            fis.close();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return str;
    }

    public List<String> readExcel(String filepath, String filename) throws Exception {
        String extString = filename.substring(filename.lastIndexOf("."));
        List<String> strs = new ArrayList();
        File excel = new File(filepath + "\\" + filename);
        Object wb;
        if (".xls".equals(extString) && !filename.startsWith("~")) {
            FileInputStream fis = new FileInputStream(excel);
            wb = new HSSFWorkbook(fis);
        } else {
            if (!".xlsx".equals(extString) || filename.startsWith("~")) {
                throw new Exception("文件类型错误!");
            }
            FileInputStream fis = new FileInputStream(excel);
            wb = new XSSFWorkbook(fis);
        }

        Sheet sheet = ((Workbook)wb).getSheetAt(0);
        int firstRowIndex = sheet.getFirstRowNum() + 1;
        int lastRowIndex = sheet.getLastRowNum();

        for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; ++rIndex) {
            Row row = sheet.getRow(rIndex);
            if (row != null) {
                int firstCellIndex = row.getFirstCellNum();
                int lastCellIndex = row.getLastCellNum();

                for(int cIndex = firstCellIndex; cIndex < lastCellIndex; ++cIndex) {
                    Cell cell = row.getCell(cIndex);
                    if (cell != null) {
                        strs.add(cell.toString());
                    }
                }
            }
        }

        return strs;
    }

    public Long doUploadFile(MultipartFile file, HttpServletRequest request) throws Exception {
        String fileName = file.getOriginalFilename();
        if (ICommons.isNullOrEmpty(fileName)) {
            throw new Exception("文件名不能为空");
        } else {
            if (fileName.indexOf("\\") != -1) {
                fileName = fileName.substring(fileName.lastIndexOf("\\"));
            }

            String ext = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
            if (ICommons.isNullOrEmpty(ext)) {
                throw new Exception("扩展名不能为空");
            } else if (!".zip".equals(ext)) {
                throw new Exception("请上传zip格式压缩文件");
            } else {
                String name = UUID.randomUUID().toString().replaceAll("-", "");
                LivePaperImport imp = (new LivePaperImport()).setImportName(fileName.substring(0, fileName.lastIndexOf("."))).setState(1).setCreateTime(System.currentTimeMillis()).setCreateUser(SessionInfo.getUserId()).setFilepath(name).setSign(1).setError(1).setStorename(name + ext);
                this.livePaperImportMapper.insert(imp);
                String filePath = request.getServletContext().getRealPath("/static/static/data/import/");
                File targetFile = new File(filePath);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }

                String storeName = name + ext;

                try {
                    File dest = new File(filePath + storeName);
                    file.transferTo(dest);
                } catch (Exception var11) {
                    var11.printStackTrace();
                    throw new Exception("上传失败");
                }

                return imp.getId();
            }
        }
    }

    public void setImportError(Long id, Integer state) {
        this.livePaperImportMapper.updateById((new LivePaperImport()).setId(id).setError(state));
    }

    public void unzip(Long id, HttpServletRequest request) throws Exception {
        LivePaperImport paperImport = (LivePaperImport)this.livePaperImportMapper.selectById(id);
        String filePath = request.getServletContext().getRealPath("/static/static/data/import/") + paperImport.getStorename();
        CompressUtils.compress(filePath);
        this.livePaperImportMapper.updateById((new LivePaperImport()).setId(id).setState(2).setError(1));
    }

    public List<LivePaperImport> listPapers() {
        return this.livePaperImportMapper.selectList((Wrapper)null);
    }

    @Async
    public void checkImport(Long id, HttpServletRequest request) {
        LivePaperImport paperImport = (LivePaperImport)this.livePaperImportMapper.selectById(id);
        Boolean isComplete = true;
        if (!ICommons.isNullOrEmpty(paperImport.getImportPersona())) {
            this.sendOkMsg("人物设定");
        } else {
            isComplete = false;
            this.sendErrorMsg("人物设定", (Long)null, 1, (Integer)null, id);
        }

        webSocketServer.sendMsg(this.getMsg(3, ""));
        List<LivePaperImportProduct> products = this.livePaperImportProductMapper.selectList((Wrapper)(new QueryWrapper((new LivePaperImportProduct()).setImportId(id))).orderByAsc("id"));

        for(Iterator var6 = products.iterator(); var6.hasNext(); webSocketServer.sendMsg(this.getMsg(3, ""))) {
            LivePaperImportProduct product = (LivePaperImportProduct)var6.next();
            Boolean flag = this.checkOneProduct(product.getId(), id);
            if (!flag) {
                this.setImportError(id, 2);
            }
        }

        this.livePaperImportMapper.updateById((new LivePaperImport()).setId(id).setState(3).setError(isComplete ? 1 : 2));
    }

    public Boolean checkOneProduct(Long productId, Long importId) {
        Boolean isComplete = true;
        LivePaperImportProduct product = (LivePaperImportProduct)this.livePaperImportProductMapper.selectById(productId);
        this.sendOkMsg(product.getProductName() + ">>开始");
        if (!ICommons.isNullOrEmpty(product.getProductDesc())) {
            this.sendOkMsg(product.getProductName() + ">>商品资料");
        } else {
            isComplete = false;
            this.sendErrorMsg(product.getProductName() + ">>商品资料", productId, 2, 4, importId);
        }

        List<LivePaperImportProductDetail> details = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setType(3).setProductId(productId)));
        if (details.size() >= 3) {
            this.sendOkMsg(product.getProductName() + ">>弹幕问题");
        } else {
            isComplete = false;
            this.sendErrorMsg(product.getProductName() + ">>弹幕问题", productId, 2, 3, importId);
        }

        Long count = this.livePaperImportProductDetailMapper.selectCount(new QueryWrapper((new LivePaperImportProductDetail()).setFirst(1).setProductId(productId)));
        if (count >= 1l) {
            this.sendOkMsg(product.getProductName() + ">>商品主图首图");
        } else {
            isComplete = false;
            this.sendErrorMsg(product.getProductName() + ">>商品主图首图", productId, 2, 2, importId);
        }

        count = this.livePaperImportProductDetailMapper.selectCount(new QueryWrapper((new LivePaperImportProductDetail()).setType(2).setAnswer(1).setFirst(2).setProductId(productId)));
        if (count >= 4l) {
            this.sendOkMsg(product.getProductName() + ">>商品主图非首图");
        } else {
            isComplete = false;
            this.sendErrorMsg(product.getProductName() + ">>商品主图非首图", productId, 2, 2, importId);
        }

        for(int i = 1; i <= 6; ++i) {
            count = this.livePaperImportProductDetailMapper.selectCount(new QueryWrapper((new LivePaperImportProductDetail()).setType(1).setSection(i).setAnswer(1).setProductId(productId)));
            if (count >= 1) {
                this.sendOkMsg(product.getProductName() + ">>" + this.getNameByType(i));
            } else {
                isComplete = false;
                this.sendErrorMsg(product.getProductName() + ">>" + this.getNameByType(i), productId, 2, 1, importId);
            }
        }

        return isComplete;
    }

    public String getNameByType(Integer type) {
        switch(type) {
            case 1:
                return "商品属性图";
            case 2:
                return "商品特点图";
            case 3:
                return "商品整体图";
            case 4:
                return "商品特写图";
            case 5:
                return "配送说明图";
            case 6:
                return "售后说明图";
            default:
                return null;
        }
    }

    public void sendOkMsg(String text) {
        webSocketServer.sendMsg(this.getMsg(1, text));

        try {
            Thread.sleep(300L);
        } catch (InterruptedException var3) {
            var3.printStackTrace();
        }

    }

    public void sendErrorMsg(String text, Long productId, Integer type, Integer detailType, Long importId) {
        this.livePaperImportErrorMapper.insert((new LivePaperImportError()).setProductId(productId));
        webSocketServer.sendMsg(this.getMsg(2, text));
    }

    public String getMsg(Integer state, String info) {
        Map<String, Object> res = new HashMap();
        res.put("state", state);
        res.put("info", info);
        return JSON.toJSONString(res);
    }

    public List<Map<String, Object>> checkNameList(Long id) {
        List<Map<String, Object>> res = new ArrayList();
        List<LivePaperImportProduct> products = this.livePaperImportProductMapper.selectList((Wrapper)(new QueryWrapper((new LivePaperImportProduct()).setImportId(id))).orderByAsc("id"));
        Map<String, Object> obj = new HashMap();
        obj.put("name", "人物设定");
        obj.put("count", 0);
        obj.put("errText", "");
        obj.put("hasEnd", false);
        res.add(obj);
        Iterator var5 = products.iterator();

        while(var5.hasNext()) {
            LivePaperImportProduct product = (LivePaperImportProduct)var5.next();
            obj = new HashMap();
            obj.put("name", product.getProductName());
            obj.put("count", 0);
            obj.put("errText", "");
            res.add(obj);
        }

        return res;
    }

    public List<LivePaperImportProduct> loadTabList(Long id) {
        List<LivePaperImportProduct> products = this.livePaperImportProductMapper.selectList(new QueryWrapper((new LivePaperImportProduct()).setImportId(id)));
        Iterator var3 = products.iterator();

        while(var3.hasNext()) {
            LivePaperImportProduct product = (LivePaperImportProduct)var3.next();
            product.setErrorCount(this.livePaperImportErrorMapper.selectCount(new QueryWrapper((new LivePaperImportError()).setProductId(product.getId()))).intValue());
        }

        return products;
    }

    public String getUrlPath(Integer type) {
        return ExamImportService.url + ImportDetail.relativePath(type);
    }

    public List<ImportDetail> loadProductDetail(Long productId) {
        List<ImportDetail> res = new ArrayList();
        LivePaperImportProduct product = (LivePaperImportProduct)this.livePaperImportProductMapper.selectById(productId);
        LivePaperImport imp = (LivePaperImport)this.livePaperImportMapper.selectById(product.getImportId());
        url = "/" + imp.getFilepath() + "/" + imp.getImportName() + "/" + product.getProductName() + "/商品图片/";
        List<LivePaperImportProductDetail> list = null;
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(3)));
        res.add(new ImportDetail(1, getUrlPath(1), list, 3, 0, 0, 0));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(2).setFirst(1).setAnswer(1)));
        res.add(new ImportDetail(2, getUrlPath(2), list, 1, 1, 1, 1));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(2).setFirst(2).setAnswer(1)));
        res.add(new ImportDetail(3, getUrlPath(3), list, 4, 2, 1, 1));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(2).setFirst(2).setAnswer(2)));
        res.add(new ImportDetail(4, getUrlPath(4), list, 0, 2, 2, 1));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(1).setAnswer(1).setSection(1)));
        res.add(new ImportDetail(5, getUrlPath(5), list, 1, 2, 1, 1));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(1).setAnswer(1).setSection(2)));
        res.add(new ImportDetail(6, getUrlPath(6), list, 1, 2, 1, 2));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(1).setAnswer(1).setSection(3)));
        res.add(new ImportDetail(7, getUrlPath(7), list, 1, 2, 1, 3));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(1).setAnswer(1).setSection(4)));
        res.add(new ImportDetail(8, getUrlPath(8), list, 1, 2, 1, 4));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(1).setAnswer(1).setSection(5)));
        res.add(new ImportDetail(9, getUrlPath(9), list, 1, 2, 1, 5));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(1).setAnswer(1).setSection(6)));
        res.add(new ImportDetail(10, getUrlPath(10), list, 1, 2, 1, 6));
        list = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(productId).setType(1).setAnswer(2)));
        res.add(new ImportDetail(11, getUrlPath(11), list, 0, 2, 2, 1));
        return res;
    }

    public void uploadErrorFile(MultipartFile file, HttpServletRequest request, LivePaperImportProductDetail detail) throws Exception {
        String fileName = file.getOriginalFilename();
        if (ICommons.isNullOrEmpty(fileName)) {
            throw new Exception("文件名不能为空");
        } else {
            if (fileName.indexOf("\\") != -1) {
                fileName = fileName.substring(fileName.lastIndexOf("\\"));
            }

            String ext = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
            if (ICommons.isNullOrEmpty(ext)) {
                throw new Exception("扩展名不能为空");
            } else if (".jsp".equals(ext)) {
                throw new Exception("what dow you want?");
            } else {
                String name = UUID.randomUUID().toString().replaceAll("-", "") + ext;
                LivePaperImportProduct product = (LivePaperImportProduct)this.livePaperImportProductMapper.selectById(detail.getProductId());
                LivePaperImport imp = (LivePaperImport)this.livePaperImportMapper.selectById(product.getImportId());
                String filepath = request.getSession().getServletContext().getRealPath("/static/static/data/import/") + imp.getFilepath() + "/" + imp.getImportName() + "/" + product.getProductName() + "/商品图片/" + ImportDetail.relativePath(detail.getType());
                file.transferTo(new File(filepath + name));
                Map<String, Integer> res = PicUtils.getImgSize(filepath + name);
                Integer type = detail.getType();
                if (type == 1) {
                    type = 3;
                } else if (Arrays.asList(2, 3, 4).contains(type)) {
                    type = 2;
                } else {
                    type = 1;
                }

                detail.setFilepath(fileName).setType(type).setRelativepath(name).setWidth((Integer)res.get("width")).setHeight((Integer)res.get("height")).setImportId(imp.getId());
                this.livePaperImportProductDetailMapper.insert(detail);
            }
        }
    }

    public void saveProductBarrage(String value, Long productId) {
        LivePaperImportProduct product = (LivePaperImportProduct)this.livePaperImportProductMapper.selectById(productId);
        this.livePaperImportProductDetailMapper.insert((new LivePaperImportProductDetail()).setProductId(productId).setType(3).setContent(value).setImportId(product.getImportId()));
    }

    public void insertTableEnd(Long id, HttpServletRequest request) throws Exception {
        Thread.sleep(2000L);
        this.setImportError(id, 1);
        List<LivePaperImportProduct> importProducts = this.livePaperImportProductMapper.selectList(new QueryWrapper((new LivePaperImportProduct()).setImportId(id)));
        LivePaperImport imp = (LivePaperImport)this.livePaperImportMapper.selectById(id);
        Iterator var5 = importProducts.iterator();

        while(var5.hasNext()) {
            LivePaperImportProduct ip = (LivePaperImportProduct)var5.next();
            LiveProduct product = (new LiveProduct()).setState(1).setCreateTime(System.currentTimeMillis()).setCreateUser(SessionInfo.getUserId()).setProductName(ip.getProductName()).setProductType(1);
            this.productMapper.insert(product);
            LiveProductItem liveProductItem = (new LiveProductItem()).setState(1).setItemTitle(ip.getProductName() + "资料").setItemContent(ip.getProductDesc()).setProductId(product.getId());
            this.liveProductItemMapper.insert(liveProductItem);
            List<LivePaperImportProductDetail> details = this.livePaperImportProductDetailMapper.selectList((Wrapper)(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(ip.getId()))).in("type", new Object[]{1, 2}));
            Iterator var10 = details.iterator();

            while(var10.hasNext()) {
                LivePaperImportProductDetail detail = (LivePaperImportProductDetail)var10.next();
                String ext = detail.getRelativepath().substring(detail.getRelativepath().lastIndexOf("."));
                String sourceFolder = ImportDetail.relativePath(this.getSourceFilePath(detail));
                String sourceFilePath = request.getSession().getServletContext().getRealPath("/static/static/data/import/") + imp.getFilepath() + "/" + imp.getImportName() + "/" + product.getProductName() + "/商品图片/" + sourceFolder + detail.getRelativepath();
                String destFilePath = request.getSession().getServletContext().getRealPath("/static/static/data/") + product.getId() + "/" + (detail.getType() == 1 ? 2 : 1) + "/";
                if (!(new File(destFilePath)).exists()) {
                    (new File(destFilePath)).mkdirs();
                }

                FileUtils.copyFile(new File(sourceFilePath), new File(destFilePath + detail.getRelativepath()));
                this.liveProductSourceMapper.insert((new LiveProductSource()).setProductId(product.getId()).setState(1).setCreateTime(System.currentTimeMillis()).setCreateUser(SessionInfo.getUserId()).setSourceName(detail.getFilepath()).setSourceExt(ext).setSourceStoreName(detail.getRelativepath()).setProductId(product.getId()).setSourceType(detail.getType() == 1 ? 2 : 1).setWidth(detail.getWidth()).setHeight(detail.getHeight()).setSection(detail.getSection()).setAnswer(detail.getAnswer()).setFirst(detail.getFirst()));
            }

            List<LivePaperImportProductDetail> barrages = this.livePaperImportProductDetailMapper.selectList(new QueryWrapper((new LivePaperImportProductDetail()).setProductId(ip.getId()).setType(3)));
            int i = 1;

            for(Iterator var18 = barrages.iterator(); var18.hasNext(); ++i) {
                LivePaperImportProductDetail bar = (LivePaperImportProductDetail)var18.next();
                this.barrageMapper.insert((new LiveBarrage()).setOrders(i).setState(1).setCreateTime(System.currentTimeMillis()).setCreateUser(SessionInfo.getUserId()).setContent(bar.getContent()).setType(5).setProductId(product.getId()));
            }
        }

        this.livePaperImportMapper.updateById((new LivePaperImport()).setId(id).setState(5));
    }

    public Integer getSourceFilePath(LivePaperImportProductDetail detail) throws Exception {
        Integer type = detail.getType();
        Integer first = detail.getFirst();
        Integer answer = detail.getAnswer();
        Integer section = detail.getSection();
        if (type == 1) {
            return answer == 1 ? 4 + section : 11;
        } else if (type == 2) {
            if (answer == 1) {
                return first == 1 ? 2 : 3;
            } else {
                return 4;
            }
        } else {
            throw new Exception("文件夹获取错误");
        }
    }

    public static void main(String[] args) {
        String path = "D:\\1\\1\\1\\1\\1\\1\\1\\";
        if (!(new File(path)).exists()) {
            (new File(path)).mkdirs();
        }

    }
}
